﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChGK.Data;

namespace CHGK.web.UI.Models
{
    public class AnswerRepositry
    {
        CHGKModelContainer _CHGKContainer;

        public AnswerRepositry()
        {
            _CHGKContainer = new CHGKModelContainer();
        }

        public AnswerRepositry(CHGKModelContainer CHGKContainer)
        {
            _CHGKContainer = CHGKContainer;
        }

        public IEnumerable<Answer> GetAnswers()
        {
            return _CHGKContainer.Answers.Cast<Answer>();
        }

        public IEnumerable<Answer> GetAnswers(Team userTeam)
        {
            Team dbTeam = _CHGKContainer.Teams.SingleOrDefault(t => t.ID == userTeam.ID);

            if (dbTeam == null)
            {
                return null;
            }
            else
            {
                return _CHGKContainer.Answers.Where(a => a.TeamID == dbTeam.ID);
            }
        }
        
        public IEnumerable<Answer> GetAnswers(Tour userTour)
        {
            Tour dbTour = _CHGKContainer.Tours.SingleOrDefault(t => t.ID == userTour.ID);

            if (dbTour == null)
            {
                return null;
            }
            else
            {
                return _CHGKContainer.Answers.Where(a => a.TourID == dbTour.ID);
            }
        }

        public IEnumerable<Answer> GetAnswers(Team userTeam, Tour userTour)
        {
            Tour dbTour = _CHGKContainer.Tours.SingleOrDefault(t => t.ID == userTour.ID);
            Team dbTeam = _CHGKContainer.Teams.SingleOrDefault(t => t.ID == userTeam.ID);

            if (dbTeam == null || dbTour == null)
            {
                return null;
            }
            else
            {
                return _CHGKContainer.Answers.Where(a => a.TeamID == dbTeam.ID && a.TourID == dbTour.ID).OrderBy(a => a.QuestionNumber);
            }
        }

        public IEnumerable<Answer> GetAnswers(int TourID, int QuestionNumber)
        {

            return _CHGKContainer.Answers.Where(a => a.TourID == TourID && a.QuestionNumber == QuestionNumber).OrderBy(a => a.IsCorrect);

        }

        public Answer GetAnswer(int id)
        {
            Answer dbAnswer = _CHGKContainer.Answers.SingleOrDefault(t => t.ID == id);

            if (dbAnswer == null)
            {
                return null;
            }
            else
            {
                return dbAnswer;
            }
        }

        public Answer GetAnswer(int TeamId, int TourId, int QuestionNumber)
        {
            Answer _Answer = _CHGKContainer.Answers.SingleOrDefault(a => a.TeamID == TeamId && a.TourID == TourId && a.QuestionNumber == QuestionNumber);
            
            if (_Answer == null)
            {
                return null;
            }
            else
            {
                return _Answer;
            }

        }

        public Answer GetFirstAnswer(int TourId, int QuestionNumber)
        {
            Answer _Answer = _CHGKContainer.Answers.FirstOrDefault(a => a.TourID == TourId && a.QuestionNumber == QuestionNumber);
            if (_Answer == null)
            {
                return null;
            }
            else
            {
                return _Answer;
            }
        }

        public void CreateAnswer(string value, short questionNumber, Boolean collectedByWeb, int tourId, int teamId)
        {
            Answer NewAnswer = new Answer();

            NewAnswer.Value = value;
            NewAnswer.QuestionNumber = questionNumber;
            NewAnswer.IsCorrect = null;
            NewAnswer.TourID = tourId;
            NewAnswer.TeamID = teamId;
            NewAnswer.CollectedByWeb = collectedByWeb;
            NewAnswer.isLate = null;
            //NewAnswer.TimeStamp = DateTime.Now;
            _CHGKContainer.Answers.AddObject(NewAnswer);
            _CHGKContainer.SaveChanges();
        } 

        public void CreateAnswer(string value, short questionNumber, Boolean collectedByWeb, int tourId, int teamId, bool IsLate)
        {
            Answer NewAnswer = new Answer();

            NewAnswer.Value = value;
            NewAnswer.QuestionNumber = questionNumber;
            NewAnswer.IsCorrect = null;
            NewAnswer.TourID = tourId;
            NewAnswer.TeamID = teamId;
            NewAnswer.CollectedByWeb = collectedByWeb;
            NewAnswer.isLate = IsLate;
            //NewAnswer.TimeStamp = DateTime.Now;
            _CHGKContainer.Answers.AddObject(NewAnswer);
            _CHGKContainer.SaveChanges();
        }


        public void UpdateAnswer(Answer userAnswer)
        {
            Answer dbAnswer = GetAnswer(userAnswer.ID);
            
            dbAnswer.Value = userAnswer.Value;
            dbAnswer.QuestionNumber = userAnswer.QuestionNumber;
            dbAnswer.IsCorrect = userAnswer.IsCorrect;
            dbAnswer.TourID = userAnswer.TourID;
            dbAnswer.TeamID = userAnswer.TeamID;
            dbAnswer.CollectedByWeb = userAnswer.CollectedByWeb;
            dbAnswer.IsCorrect = userAnswer.IsCorrect;
           
            _CHGKContainer.SaveChanges();

        }

        public void UpdateAnswer(int ID, bool IsCorrect)
        {
            Answer dbAnswer = GetAnswer(ID);
           
            dbAnswer.IsCorrect = IsCorrect;
           
            _CHGKContainer.SaveChanges();
        }

        public void UpdateAnswer(bool IsLate, int ID)
        {
            Answer dbAnswer = GetAnswer(ID);

            dbAnswer.isLate = IsLate;

            _CHGKContainer.SaveChanges();
            
        }

        public void DeleteAnswer(int id)
        {
            Answer dbAnswer = GetAnswer(id);
           
            _CHGKContainer.Answers.DeleteObject(dbAnswer); 
            _CHGKContainer.SaveChanges();

        }

    }
}